package cn.neusoft.dao.impl;


import cn.neusoft.bean.Food;
import cn.neusoft.dao.IFoodDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.BusinessQuery;
import cn.neusoft.query.FoodQuery;
import cn.neusoft.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodDao implements IFoodDao {
    private QueryRunner queryRunner;

    public FoodDao(){
        queryRunner = JdbcUtils.getQueryRunner();
    }
    @Override
    public int add(Food food) throws SQLException {
        String sql="insert into t_food(foodId, foodName, foodExplain, foodImg, foodPrice, businessId, remarks) values (?,?,?,?,?,?,?)";
        return queryRunner.update(sql,
                food.getFoodId(),
                food.getFoodName(),
                food.getFoodExplain(),
                food.getFoodImg(),
                food.getFoodPrice(),
                food.getBusinessId(),
                food.getRemarks());
    }
    @Override
    public int update(Food food)throws  SQLException{
        String sql="update t_food set foodName=?, foodExplain=?, foodImg=?, foodPrice=?, businessId=?, remarks=? where foodId=?";
        return queryRunner.update(sql,
                food.getFoodName(),
                food.getFoodExplain(),
                food.getFoodImg(),
                food.getFoodPrice(),
                food.getBusinessId(),
                food.getRemarks(),
                food.getFoodId());

    }
    @Override
    public int delete(Integer id)throws  SQLException{
        String sql="delete from t_food where foodId=?";
        return queryRunner.update(sql,id);  }

    @Override
    public Food getById(Integer id) throws SQLException {
        String sql="select foodId, foodName, foodExplain, foodImg, foodPrice, businessId, remarks from t_food where foodId=?";

        return queryRunner.query(sql,new BeanHandler<Food>(Food.class),id);
    }

    @Override
    public List<Food> getAll() throws SQLException {
        String sql="select foodId, foodName, foodExplain, foodImg, foodPrice, businessId, remarks from t_food";

        return queryRunner.query(sql,new BeanListHandler<>(Food.class));
    }


    @Override
    public int getCount(BaseQuery baseQuery) throws SQLException {
        FoodQuery query=(FoodQuery) baseQuery;
        List<Object> params=new ArrayList<>();
        String sql="select count(foodId) from t_food where 1=1";
        if(query.getFoodName()!=null){
            sql+=" and foodName like concat('%',?,'%')";
        params.add(query.getFoodName());
        }
        if(query.getFoodExplain()!=null){
            sql+=" and foodExplain like concat('%',?,'%')";
            params.add(query.getFoodExplain());}
        System.out.println("查询总记录："+sql);
        System.out.println("参数集合："+params);
        return queryRunner.query(sql, new ScalarHandler<Long>(), params.toArray()).intValue();
    }

    @Override
    public List<Food> getByPage(BaseQuery baseQuery) throws SQLException {
        FoodQuery query=(FoodQuery) baseQuery;
        List<Object> params=new ArrayList<>();
        String sql="select foodId, foodName, foodExplain, foodImg, foodPrice, businessId, remarks from t_food where 1=1 ";
        if(query.getFoodName()!=null){
            sql+=" and foodName like concat('%',?,'%')";
            params.add(query.getFoodName());
        }
        if(query.getFoodExplain()!=null){
            sql+=" and foodExplain like concat('%',?,'%')";
            params.add(query.getFoodExplain());}
        sql+="limit ?,?";
        params.add(query.getStartIndex());
        params.add(query.getPageCount());
        return  queryRunner.query(sql,new BeanListHandler<Food>(Food.class),params.toArray());
    }
}

